package com.baibei.ebec.user.ticket;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.model.CouponInfo;
import com.baibei.model.RankingInfo;
import com.baibei.module.AppRouter;
import com.baibei.ui.AppUI;
import com.blankj.utilcode.utils.TimeUtils;
import com.blankj.utilcode.utils.ToastUtils;
import com.rae.swift.Rx;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.baibei.ebec.user.ticket.TicketAdapter.ViewType.EMPTY;
import static com.baibei.ebec.user.ticket.TicketAdapter.ViewType.INVALID_TITLE;
import static com.baibei.ebec.user.ticket.TicketAdapter.ViewType.VALID_TITLE;

/**
 * 适配器
 * Created by shenminjie on 2017/9/1.
 * mail:shenminjie@100bei.com
 */

class TicketAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CouponInfo> va;
    private List<RankingInfo> rankingInfos;

    /**
     * 类型标签
     * 有效title
     * item
     * 无效title
     */
    enum ViewType {
        VALID_TITLE, ITEM, INVALID_TITLE, EMPTY
    }

    private SparseArray<ViewType> mTitles;
    private SparseArray<CouponInfo> mCoupons;
    private Context context;

    TicketAdapter(Context context) {
        mTitles = new SparseArray<>();
        mCoupons = new SparseArray<>();
        this.context = context;
    }

    /**
     * 设置数据信息
     *
     * @param valides   valides
     * @param invalides invalides
     */
    void setInfos(List<CouponInfo> valides, List<CouponInfo> invalides ) {
        va = valides;
        mTitles.clear();
        mCoupons.clear();
        int position = 0;
        if (!Rx.isEmpty(valides)) {
            mTitles.put(position, VALID_TITLE);
            position++;
            for (CouponInfo couponInfo : valides) {
                mCoupons.put(position, couponInfo);
                position++;
            }
        }
        if (!Rx.isEmpty(invalides)) {
            mTitles.put(position, INVALID_TITLE);
            position++;
            for (CouponInfo couponInfo : invalides) {
                mCoupons.put(position, couponInfo);
                position++;
            }
        }
    }

    /**
     * 设置排行榜信息
     * @return
     */
    void setRankInfos(List<RankingInfo> rankingInfoList){
        rankingInfos = rankingInfoList;
    }

    @Override
    public int getItemViewType(int position) {
        if (mTitles.size() + mCoupons.size() == 0) {
            return EMPTY.ordinal();
        }
        if (mTitles.get(position) != null) {
            return mTitles.get(position).ordinal();
        }
        if (mCoupons.get(position) != null) {
            return ViewType.ITEM.ordinal();
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        if (viewType == ViewType.VALID_TITLE.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ticket_available, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        } else if (viewType == ViewType.INVALID_TITLE.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ticket_unavailable, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        } else if (viewType == ViewType.ITEM.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ticket, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        } else if (viewType == ViewType.EMPTY.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty_view, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            final CouponInfo info = mCoupons.get(position);
//            viewHolder.tvQuantity.setText(String.valueOf(info.getFaceValue()));
            viewHolder.tvTitle.setText(info.getCouponName());
            viewHolder.tvDate.setText(String.format("有效期: %s - %s",
                    TimeUtils.millis2String(info.getEffectiveTime(), "yyyy.MM.dd"),
                    TimeUtils.millis2String(info.getExpireTime(), "yyyy.MM.dd")));
//            viewHolder.layoutTicketQuantity.setEnabled(CouponInfo.STATUS_GIVED.equals(info.getStatus()));
            if (info.getFaceValue() > 1) {
                viewHolder.layoutTicketQuantity.setImageResource(CouponInfo.STATUS_GIVED.equals(info.getStatus()) ? R.drawable.light_quan2 : R.drawable.gray_quan2);
            } else {
                viewHolder.layoutTicketQuantity.setImageResource(CouponInfo.STATUS_GIVED.equals(info.getStatus()) ? R.drawable.light_quan : R.drawable.gray_quan);
            }
            //可用优惠券点击跳转首页
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (va == null || position > va.size()) {
                        return;
                    }
                    CouponInfo couponInfo = va.get(position-1);
                    String productId = couponInfo.getProduct().getProductId();
                    if (rankingInfos==null) return;
                    for (int i = 0 ; i < rankingInfos.size() ; i++){
                        RankingInfo rankingInfo = rankingInfos.get(i);
                        if (productId.equals(rankingInfo.getProductId())){
                            AppRouter.routeToTrade(context, rankingInfo.getEstimateTime(), rankingInfo, rankingInfo.getSaleTips());
                            return;
                        } else if (i == rankingInfos.size()-1){
                            AppUI.toast(context,"找不到对应的商品");
                        }
                    }
//                    AppRouter.routeToMain(context, 0);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = mTitles.size() + mCoupons.size();


        if (count == 0) {
            return 1;
        }
        return count;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.layout_ticket_quantity)
        ImageView layoutTicketQuantity;
        @BindView(R2.id.tv_title)
        TextView tvTitle;
        @BindView(R2.id.tv_date)
        TextView tvDate;
        @BindView(R2.id.btn)
        TextView btn;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
